package TitleScreen;

import java.net.SocketException;

import javax.swing.ImageIcon;

import Message.MessageTypes;
import Message.XMLcontent;
import Message.XMLmessage;
import Player.mPlayer;
import ServerListener.ServerListener;

/**
 * 
 * @author Christopher Vogel
 */

public class mTitleScreen {

	public mTitleScreen() {
		ServerListener.TitleScreen=this;
		
	}
	
	/**
	 * Versucht einen neuen Benutzer zu registrieren.
	 * @return ErrorMessage
	 * @throws SocketException
	 * @author Christopher Vogel
	 */		
	public synchronized String register(String UserName, String password) throws SocketException {
		if (UserName.isEmpty() || password.isEmpty()) {
			return "Please fill in all requirments fields";
		}
		ServerListener.get().connect();
		mPlayer player = new mPlayer(UserName, password);
		String validate = null;
		XMLcontent content = new XMLcontent();
		content.setPlayer(player);
		XMLmessage message = new XMLmessage(MessageTypes.UserRegistration,
				content, player);

		try {
			message.send(ServerListener.Socket);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e.getLocalizedMessage());
		} // must register to socket at titlescreen

		XMLmessage reference = message.getReference();

		if (reference == null) {
			validate = "Server Error Try Again!";
		} else if (reference.getType() != MessageTypes.Confirmation) {
			validate = reference.getContent().getString();
		} else if (validate==null) {
			ServerListener.Player = player;
		}
		return validate;
	}

	/**
	 * Versucht einen Benutzer in den Server einzuloggen.
	 * @return ErrorMessage
	 * @author Christopher Vogel
	 */	

	public synchronized String login(String username, String password) throws SocketException {

		String registerMsg = validateUser(username, password);
		if (registerMsg == null) {
			return null;

		} else {
			return registerMsg;
		}

	}

	
	/**
	 * Teil der Log-in Methode
	 * @author Christopher Vogel
	 * @return ErrorMessage
	 */	
	
	public String validateUser(String UserName, String password) throws SocketException {
		String validate = null;
		if (UserName.isEmpty() || password.isEmpty()) {
			return "Please fill in all requirments fields";
		} else {
			ServerListener.get().connect();

			mPlayer Player = new mPlayer(UserName, password);
			XMLcontent Content = new XMLcontent();
			Content.setPlayer(Player);

			XMLmessage msg = new XMLmessage(MessageTypes.UserLogin, Content, /* sender */
			Player);

			try {
				Thread.sleep(30);
				msg.send(ServerListener.Socket);
				System.out.println("UserLogin wurde verschickt");
			} catch (Exception e) {
				System.err.println(e.getMessage());
				e.printStackTrace();
			}
			XMLmessage reference = msg.getReference();
			if (reference == null) {
				validate = "Server Error Try Again!";
			} else if (reference.getType() != MessageTypes.Confirmation) {
				validate = reference.getContent().getString();
			}
			if (validate==null) {
				ServerListener.Player = reference.getContent().getPlayer();
			}
		}
		return validate;

	}

}
